/**
 * Created by Administrator on 2017/7/3.
 */
var vm_newsList = avalon.define({
    $id: "newsList",
    bannerArr: [],
    columnContent: [],
    columnTitle: [],
    columnLenth: [],
    currentColumnData: [],
    newsImgs: [],
    currentNewsImgs: [],
    currentColumnDataStart: 0,
    currentIndex: 0,
    tnTab: function () {
        var n = 0;
        var self = this;
        n = ( self.currentIndex == 0 ? 0 : self.currentIndex);
        $(".tnTab li").eq(n).addClass("active").siblings().removeClass("active")
        $(".tnTab li a").click(function () {
            self.currentIndex = $(".tnTab li a").index($(this));
            // $(".tnTabContent").eq(n).addClass("current").siblings().removeClass("current");
            $(".tnTab li").eq(self.currentIndex).addClass("active").siblings().removeClass("active");
            self.cutArr(self.currentIndex);
        })
    },
    cutArr: function (n) {//截取对应分类的数据
        if (n == 0) {
            this.currentColumnData = [];
            this.currentNewsImgs = [];
            this.currentColumnData = this.columnContent;
            this.currentNewsImgs = this.newsImgs
        } else {
            var start = 0;
            var end = 0;
            for (var i = 0, len = this.columnLenth.length; i < len; i++) {
                if (i < n - 1) {
                    start += this.columnLenth[i];
                }
                if (i < n) {
                    end += this.columnLenth[i];
                }

            }
            console.log(start + "----" + end);
            this.getCurrentData(start, end);
        }
    },
    getCurrentData: function (start, end) {
        console.log(start + ";;;" + end);
        this.currentColumnData = [];
        this.currentNewsImgs = [];
        this.currentColumnData = this.columnContent.slice(start, end);
        this.currentNewsImgs = this.newsImgs.slice(start, end);
    },
    formatNewsContent: function (content) {
        var _content = $.frontEngine.removeHTMLTag(content);
        _content = _content.substr(0, 130) + "......";
        return _content;
    },
    recentNewsGoPage: function (index) {
        console.log("this.currentColumnData[index].id=" + this.currentColumnData[index].id + ";;this.currentIndex==" + this.currentIndex);
        session_Storage("newsCurrentIndex", this.currentIndex)
        window.location.href = "content.html?id=" + this.currentColumnData[index].id;
    }
})
init()
function init() {
    getColumnId();
    var tempNewsIndex = get_Storage("newsCurrentIndex");
    console.log("tempNewsIndex==" + tempNewsIndex)
    if (tempNewsIndex != null) {
        vm_newsList.currentIndex = tempNewsIndex;
    }
}

function getColumnId() {
    var pageId = getPageId('新闻中心');
    var ColumnIdArr = [];
    console.log('pageId=' + pageId);
    var _basePath = getCatelogUrl(pageId);
    console.log(_basePath);
    $.frontEngineAjax.ajaxRequest(function (data) {
        var tempOption;
        for (var i = 0, len = data.length; i < len; i++) {
            var name = conformName(data[i].name);
            tempOption = {
                "name": name,
                "number": data[i].number
            }
            ColumnIdArr[i] = tempOption;
        }
        console.log(ColumnIdArr);
        getColumnData(ColumnIdArr)
    }, _basePath)
}

var index = 0;
function getColumnData(ColumnIdArr) {
    var lenth = ColumnIdArr.length;
    console.log(lenth);
    var _basePath = getDataUrl(ColumnIdArr[index].number);
    console.log("getColumnData _basePath=" + _basePath);
    $.frontEngineAjax.ajaxRequest(function (data) {
        console.log('data[0].length = ' + data.length)
        if (index == 0) {
            for (var i = 0, len = data.length; i < len; i++) {
                var tempImgUrl = data[i].imageUrl;
                vm_newsList.bannerArr.push(spliceImgUrl(tempImgUrl));
            }
            console.log('vm_newsList.bannerArr=' + vm_newsList.bannerArr)
        } else {
            vm_newsList.currentColumnData = vm_newsList.columnContent = vm_newsList.columnContent.concat(data);
            var tempName = data[0].catelog.name;
            vm_newsList.columnTitle.push(conformName(tempName));
            vm_newsList.columnLenth.push(data.length);
            console.log(vm_newsList.columnContent.length + ";;;" + vm_newsList.columnLenth);
        }
        if (index < lenth - 1) {
            index++;
            getColumnData(ColumnIdArr);
        } else {
            getNewsImg()
        }
        vm_newsList.cutArr(vm_newsList.currentIndex);//从内容页返回记录进去时的分类
    }, _basePath);

}

function getNewsImg() {
    for (var i = 0, len = vm_newsList.columnContent.length; i < len; i++) {
        var tempImgUrl = vm_newsList.columnContent[i].imageUrl;
        vm_newsList.newsImgs.push(spliceImgUrl(tempImgUrl));
    }
}